module.exports = (app) => {
  const express = require("express");
  const router = express.Router();
  const Role = require("../model/rolesModel");
  let mult = require("connect-multiparty")({
    uploadDir: "F:/毕设/img",
  });
  app.use(express.json()); // 识别json数据
  // 用户登录
  router.post("/postLogin", async (q, s) => {
    console.log(q.body);
    let user = await Role.findOne({ phone: q.body.phone });
    if (user != null) {
      if (user.password == q.body.password) {
        s.send(user);
      } else {
        s.send("密码错误!");
      }
    } else {
      s.send(false);
    }
  });

  // 用户注册
  router.post("/postRegist", async (q, s) => {
    let user = await Role.findOne({ phone: q.body.phone });
    if (user != null) {
      s.send(false);
    } else {
      const model = await Role.create(q.body);
      s.send(model);
    }
  });

  // 如果是普通用户注册，让他填写基本信息
  router.post("/postDetailData", async (q, s) => {
    let user = await Role.findOne({ phone: q.body.phone });
    if (user == null) {
      s.send(false);
    } else {
      user.name = q.body.name == undefined ? user.name : q.body.name;
      user.gender = q.body.gender == undefined ? user.gender : q.body.gender;
      user.sign = q.body.sign == undefined ? user.sign : q.body.sign;
      user.portrait_url =
        q.body.portrait_url == undefined
          ? user.portrait_url
          : q.body.portrait_url;
      await user.save();
      s.send(user);
    }
  });

  // 用户上传图片
  router.post("/uploadImg/:phone", mult, async (q, s) => {
    console.log(q.params);
    let user = await Role.findOne({ phone: q.params.phone });
    if (user == null) {
      s.sendStatus(202);
    } else {
      let name = q.files.portrait_url.path.split("\\").reverse()[0];
      user.portrait_url = name;
      await user.save();
      s.send(name);
    }
  });
  //首页获取所有信息
  router.get("/getAllInfomation/:phone", async (q, s) => {
    let user = await Role.findOne({ phone: q.params.phone });
    if (user == null) {
      s.send(false);
    } else {
      s.send(user);
    }
  });

  // 修改手机号
  router.post("/modifyPhone", async (q, s) => {
    let user = await Role.findOne({ phone: q.body.modify_phone });
    if (user != null) {
      s.send(false);
    } else {
      let modify = await Role.findOne({ phone: q.body.phone });
      modify.phone = q.body.modify_phone;
      modify.save();
      s.send(modify);
    }
  });

  router.get("/getAllUser", async (q, s) => {
    let user = await Role.find();
    s.send(user);
  });

  //修改基本信息
  //不作修改的值不用传
  router.post("/postUserUpdateData", async (q, s) => {
    let user = await Role.findOne({ phone: q.body.phone });
    if (user == null) {
      s.send(false);
    } else {
      user.phone =
        q.body.modify_phone == undefined ? user.phone : q.body.modify_phone;
      user.password =
        q.body.password == undefined ? user.password : q.body.password;
      user.name = q.body.name == undefined ? user.name : q.body.name;
      user.frozen = q.body.frozen == undefined ? user.frozen : q.body.frozen;
      user.portrait_url =
        q.body.portrait_url == undefined
          ? user.portrait_url
          : q.body.portrait_url;
      user.account =
        q.body.account == undefined ? user.account : q.body.account;
      user.credits =
        q.body.credits == undefined ? user.credits : q.body.credits;
      user.money = q.body.money == undefined ? user.money : q.body.money;
      user.gender = q.body.gender == undefined ? user.gender : q.body.gender;
      user.sign = q.body.sign == undefined ? user.sign : q.body.sign;
      user.save();
      s.send(user);
    }
  });
  // 用户添加收货地址
  router.post("/postUserAddAddress", async (q, s) => {
    let user = await Role.findOne({ phone: q.body.phone });
    if (user == null) {
      s.send(false);
    } else {
      let first = false;
      if (user.user.address.length == 0) {
        first = true;
      }
      user.user.address.push({
        default: first,
        name: q.body.name,
        ads_phone: q.body.ads_phone,
        province: q.body.province,
        city: q.body.city,
        area: q.body.area,
        town: q.body.town,
        street: q.body.street,
        detail: q.body.detail,
        address_crood: q.body.address_crood,
      });
      await user.save();
      s.send(user);
    }
  });

  // 普通用户修改收获地址
  // 不作修改的值不用传(这里不修改default，请在下面的接口修改)
  router.post("/postUserUpdateAddress", async (q, s) => {
    let user = await Role.findOne({ phone: q.body.phone });
    if (user == null) {
      s.send({
        msg: "用户不存在！",
        success: false,
      });
    } else {
      let flag = false;
      user.user.address.forEach((v) => {
        if (v._id == q.body.id) {
          console.log(q.body.address_crood);
          flag = true;
          v.name = q.body.name == undefined ? v.name : q.body.name;
          v.ads_phone =
            q.body.ads_phone == undefined ? v.name : q.body.ads_phone;
          v.province =
            q.body.province == undefined ? v.province : q.body.province;
          v.city = q.body.city == undefined ? v.city : q.body.city;
          v.area = q.body.area == undefined ? v.area : q.body.area;
          v.town = q.body.town == undefined ? v.town : q.body.town;
          v.street = q.body.street == undefined ? v.street : q.body.street;
          v.detail = q.body.detail == undefined ? v.detail : q.body.detail;
          v.address_crood =
            q.body.address_crood == "" ? v.address_crood : q.body.address_crood;
        }
      });
      if (flag == true) {
        user.save().then(() => {
          s.send(user);
        });
      } else {
        s.send({
          msg: "地址不存在",
          success: false,
        });
      }
    }
  });

  // 设为默认地址（默认地址只能存在一个）
  router.get("/setDefaultAddress/:id/:phone", async (q, s) => {
    let user = await Role.findOne({ phone: q.params.phone });
    if (user == null) {
      s.send(false);
    } else {
      user.user.address.forEach((v) => {
        if (v.default == true) {
          v.default = false;
        }
        if (v._id == q.params.id) {
          v.default = true;
        }
      });
      user.save();
      s.send(user);
    }
  });

  // 删除地址
  router.get("/deleteAddress/:id/:phone", async (q, s) => {
    let user = await Role.findOne({ phone: q.params.phone });
    let setFirst = false;
    if (user == null) {
      s.send(false);
    } else {
      user.user.address.forEach((v, i) => {
        if (v._id == q.params.id) {
          if (v.default == true) {
            if (user.user.address.length > 0) {
              setFirst = true;
            }
            user.user.address.splice(i, 1);
          } else {
            user.user.address.splice(i, 1);
          }
        }
      });
      if (setFirst == true) {
        user.user.address[0].default = true;
      }
      user.save();
      s.send(user);
    }
  });

  // 回收员定位工作地方
  router.post("/postWorkPlace", async (q, s) => {
    let user = await Role.findOne({ phone: q.body.phone });
    if (user == null) {
      s.send(false);
    } else {
      user.collector.work_city = q.body.work_city;
      user.collector.work_area = q.body.work_area;
      user.save().then(() => {
        s.send(user);
      });
    }
  });
  app.use("/account", router);
};
